import numpy as np
import cv2


lower_blue = np.array([0,0,0])
upper_blue = np.array([170,255,46])
cap = cv2.VideoCapture(0)

while(1):
	e1 = cv2.getTickCount()
	ret , frame = cap.read()
	e2 = cv2.getTickCount()
	a =	(e2 - e1)/cv2.getTickFrequency()
	print "read",a

	e1 = cv2.getTickCount()
	hsv = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)
	e2 = cv2.getTickCount()
	b =	(e2 - e1)/cv2.getTickFrequency()
	print "hsv",b

	e1 = cv2.getTickCount()
	mask = cv2.inRange(hsv,lower_blue,upper_blue)
	e2 = cv2.getTickCount()
	c  =	(e2 - e1)/cv2.getTickFrequency()
	print "mask.shape",mask.shape
	e1 = cv2.getTickCount()
	gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
	e2 = cv2.getTickCount()
	d =	(e2 - e1)/cv2.getTickFrequency()
	print "gray",d

	e1 = cv2.getTickCount()
	median = cv2.medianBlur(mask,5)
	e2 = cv2.getTickCount()
	e =	(e2 - e1)/cv2.getTickFrequency()
	print "median",e	

	e1 = cv2.getTickCount()
	res = cv2.bitwise_and(frame , frame ,mask =mask)
	e2 = cv2.getTickCount()
	m =	(e2 - e1)/cv2.getTickFrequency()
	print "bits",m

	print "time :",a+b+c+d+e+m	

	print frame.shape

	e1 = cv2.getTickCount()
	cv2.imshow('frame',frame)
	e2 = cv2.getTickCount()
	f =	(e2 - e1)/cv2.getTickFrequency()
	print "show_frame",f	


	e1 = cv2.getTickCount()
	cv2.imshow("mask",mask)
	e2 = cv2.getTickCount()
	g =	(e2 - e1)/cv2.getTickFrequency()
	print "show_mask",g	

	e1 = cv2.getTickCount()
	cv2.imshow("median",median)
	e2 = cv2.getTickCount()
	h =	(e2 - e1)/cv2.getTickFrequency()
	print "show_median",h	

	e1 = cv2.getTickCount()
	cv2.imshow("gray",gray)
	e2 = cv2.getTickCount()
	i =	(e2 - e1)/cv2.getTickFrequency()
	print "show_gray",i	

	e1 = cv2.getTickCount()
	cv2.imshow("res",res)
	e2 = cv2.getTickCount()
	n =	(e2 - e1)/cv2.getTickFrequency()
	print "show_res",n	

	print "all_time",a+b+c+d+e+f+g+h+i+m+n	

	print "youhua:",cv2.useOptimized()	
	if cv2.waitKey(1) & 0xFF == ord('q'):
		break
cap.release()
cv2.destroyAllWindows()
